using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class GameUI : MonoBehaviour
{
    [Header("����")]
    public Text scoreText;
    [HideInInspector] public int score;
    public int target_score;
    public GameObject win_window;
    [Header("分数")]
    [HideInInspector] public bool game_play;
    public Text timeText;
    private float current_time;
    [Header("时间")]
    public bool create_obstacle;
    public GameObject obstacle;

    void Start()
    {
        ResetScore();
        print(PlayerPrefs.GetInt("level", 0));
    }

    private void Update()
    {
        scoreText.text = "分数：" + score.ToString();
        if(game_play)
        {
            current_time += Time.deltaTime * 10;
        }
        timeText.text = "时间：" + ((int)current_time).ToString();

        
    }

    public void ResetScore()
    {
        score = 0;
        current_time = 0f;
    }

    public void AddScore()
    {
        score++;

        if(create_obstacle)
        {
            Instantiate(obstacle, this.gameObject.transform.position, Quaternion.identity);
        }

        if(score >= target_score)
        {
            win_window.SetActive(true);
            game_play = false;
            GameObject.Find("snake").GetComponent<Snake>().direction = Vector2.zero;
        }
    }

    public void Return_Game()
    {
        SceneManager.LoadScene("Select");
    }

    public void Victory_Game(int num)
    {
        
        SceneManager.LoadScene("Select");
        if(num > PlayerPrefs.GetInt("level", 0))
        {
            PlayerPrefs.SetInt("level", num);
            PlayerPrefs.Save();
        }
    }
    public void Save_score()
    {
        if(score > PlayerPrefs.GetInt("score", 0))
        {
            PlayerPrefs.SetInt("score", score);
            PlayerPrefs.Save();
            Debug.Log("存储成功");
        }
    }
}
